﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using NoteExpress;

namespace neut
{
    public partial class FoldersDlg : Form
    {
        public FoldersDlg()
        {
            InitializeComponent();
        }

        void AddNode(TreeNodeCollection nodes,INEFolder folder)
        {
            for(var i=0;i<folder.Count;i++)
            {
                var tmp=folder.get_Children(i);
                var t=nodes.Add(tmp.Title);
                t.Tag=tmp;
                AddNode(t.Nodes,tmp);
            }
        }

        public INEItem NEItem
        { get; set; }

        INEFolder _NEFolder;
        public INEFolder NEFolder
        {
            get { return _NEFolder; }
            set
            {
                _NEFolder = value;
                treeView1.Nodes.Clear();
                var t=treeView1.Nodes.Add(value.Title);
                t.Tag = value;
                AddNode(t.Nodes, value);
                treeView1.ExpandAll();
            }
        }

        private void selectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode.Tag.ToNEFolder().Activate();
            (new NEHost2()).MainForm.Show();
        }

        private void newToolStripMenuItem_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                var tmp = (new NEHost2()).Libraries.ActiveFolder;
                _NEFolder=treeView1.SelectedNode.Tag.ToNEFolder().Add(sender.ToString());
                _NEFolder.LinkItem(NEItem);
                tmp.Activate();
                this.Close();
            }
        }
    }
}
